package com.aiyige.page.my.merchandisemanagement.adapter;

import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.arouter.Router;
import com.aiyige.base.db.table.LearnVideoModel;
import com.aiyige.base.db.table.MajorCourseDBModel;
import com.aiyige.base.db.table.TraingCardDBModel;
import com.aiyige.base.db.table.TraingClassDBModel;
import com.aiyige.model.moment.entity.MajorCourse;
import com.aiyige.page.detail.DetailPage;
import com.aiyige.page.my.merchandisemanagement.mmlearnvideo.adapter.MMLearnVideoAdapter;
import com.aiyige.page.my.merchandisemanagement.mmmajorcourse.adapter.MMMajorCourseAdapter;
import com.aiyige.page.my.merchandisemanagement.mmtraingcard.adapter.MMTraingCardAdapter;
import com.aiyige.page.my.merchandisemanagement.mmtraingclass.adapter.MMTraingClassAdapter;
import com.aiyige.page.my.merchandisemanagement.model.SearchResultItem;
import com.aiyige.page.my.merchandisemanagement.model.TabItem;
import com.aiyige.page.publish.util.PublishUtil;
import com.aiyige.utils.AccountUtil;
import com.aiyige.utils.ListUtil;
import com.aiyige.utils.StringUtils;
import com.aiyige.utils.ToastX;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zhuyakun on 2017/11/21.
 */

public class MMSearchResultAdapter extends BaseQuickAdapter<SearchResultItem, BaseViewHolder> {
    FragmentActivity mActivity;

    public MMSearchResultAdapter(FragmentActivity activity) {
        super(0, new LinkedList<SearchResultItem>());
        mActivity = activity;
    }

    @Override
    protected void convert(BaseViewHolder helper, SearchResultItem item) {
        switch (item.getItemType()) {
            case PublishUtil.PARENT_TYPE_LEARN_VIDEO:
                ((LearnVideoViewHolder) helper).bindData(item);
                break;
            case PublishUtil.PARENT_TYPE_MAJOR_COURESE:
                ((MajorCourseViewHolder) helper).bindData(item);
                break;
            case PublishUtil.PARENT_TYPE_TRAING_CLASS:
                ((TraingClassViewHolder) helper).bindData(item);
                break;
            case PublishUtil.PARENT_TYPE_TRAING_CARD:
                ((TraingCardViewHolder) helper).bindData(item);
                break;
        }
    }

    @Override
    protected int getDefItemViewType(int position) {
        return mData.get(position).getItemType();
    }

    @Override
    protected BaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {
        BaseViewHolder baseViewHolder;
        switch (viewType) {
            case PublishUtil.PARENT_TYPE_LEARN_VIDEO:
                baseViewHolder = new LearnVideoViewHolder(getItemView(R.layout.mm_search_result, parent));
                break;
            case PublishUtil.PARENT_TYPE_MAJOR_COURESE:
                baseViewHolder = new MajorCourseViewHolder(getItemView(R.layout.mm_search_result, parent));
                break;
            case PublishUtil.PARENT_TYPE_TRAING_CLASS:
                baseViewHolder = new TraingClassViewHolder(getItemView(R.layout.mm_search_result, parent));
                break;
            case PublishUtil.PARENT_TYPE_TRAING_CARD:
                baseViewHolder = new TraingCardViewHolder(getItemView(R.layout.mm_search_result, parent));
                break;
            default:
                baseViewHolder = new BaseViewHolder(getItemView(R.layout.wild_card_item, parent));
                break;
        }
        return baseViewHolder;
    }


    public class LearnVideoViewHolder extends BaseViewHolder {
        @BindView(R.id.searchResultTitleTv)
        TextView searchResultTitleTv;
        @BindView(R.id.moreLayout)
        LinearLayout moreLayout;
        @BindView(R.id.recyclerView)
        RecyclerView recyclerView;
        @BindView(R.id.noResultTv)
        TextView noResultTv;
        MMAdapter mmLearnVideoAdapter;


        public LearnVideoViewHolder(View view) {
            super(view);
            mmLearnVideoAdapter = new MMAdapter(mActivity);
            mmLearnVideoAdapter.setEnableHighlight(true);
            ButterKnife.bind(this, view);
            recyclerView.setLayoutManager(new LinearLayoutManager(itemView.getContext()));
            mmLearnVideoAdapter.bindToRecyclerView(recyclerView);
            mmLearnVideoAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    LearnVideoModel item = (LearnVideoModel) mmLearnVideoAdapter.getItem(position);
                    switch (item.getProgressStatus()) {
                        case PublishUtil.PROGRESS_STATUS_SERVER_TRANSCODING:
                            ToastX.show(R.string.server_transcoding);
                            break;
                        default:
                            Router.start(item.getRouter());
                            break;
                    }
                }
            });
            addOnClickListener(R.id.moreLayout);
        }

        public void bindData(SearchResultItem data) {
            if (data.getTotalLearnVideo() != 0) {
                searchResultTitleTv.setText(StringUtils.getString(R.string.publish_learn_video) + "(" + data.getTotalLearnVideo() + ")");
            } else {
                searchResultTitleTv.setText(StringUtils.getString(R.string.publish_learn_video));
            }
            if (ListUtil.isEmpty(data.getLearnVideoModelList())) {
                moreLayout.setVisibility(View.INVISIBLE);
                noResultTv.setVisibility(View.VISIBLE);
                mmLearnVideoAdapter.setNewData(new LinkedList<MultiItemEntity>());
            } else {
                if (data.getTotalLearnVideo() <= SearchResultItem.MAX_SAMPLE_NUM) {
                    moreLayout.setVisibility(View.INVISIBLE);
                } else {
                    moreLayout.setVisibility(View.VISIBLE);
                }
                noResultTv.setVisibility(View.INVISIBLE);
            }
            List<MultiItemEntity> destList = new LinkedList<>();
            destList.addAll(data.getLearnVideoModelList().subList(0, Math.min(data.getLearnVideoModelList().size(), SearchResultItem.MAX_SAMPLE_NUM)));
            mmLearnVideoAdapter.setNewData(destList);
        }
    }

    public class MajorCourseViewHolder extends BaseViewHolder {
        @BindView(R.id.searchResultTitleTv)
        TextView searchResultTitleTv;
        @BindView(R.id.moreLayout)
        LinearLayout moreLayout;
        @BindView(R.id.recyclerView)
        RecyclerView recyclerView;
        @BindView(R.id.noResultTv)
        TextView noResultTv;
        MMAdapter mmMajorCourseAdapter;


        public MajorCourseViewHolder(View view) {
            super(view);
            mmMajorCourseAdapter = new MMAdapter(mActivity);
            mmMajorCourseAdapter.setEnableHighlight(true);
            ButterKnife.bind(this, view);
            recyclerView.setLayoutManager(new LinearLayoutManager(itemView.getContext()));
            mmMajorCourseAdapter.bindToRecyclerView(recyclerView);
            mmMajorCourseAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    MajorCourseDBModel item = (MajorCourseDBModel) mmMajorCourseAdapter.getItem(position);
                    switch (item.getProgressStatus()) {
                        case PublishUtil.PROGRESS_STATUS_SERVER_TRANSCODING:
                            ToastX.show(R.string.server_transcoding);
                            break;
                        default:
                            Router.start(item.getRouter());
                            break;
                    }
                }
            });
            addOnClickListener(R.id.moreLayout);
        }

        public void bindData(SearchResultItem data) {
            if (data.getTotalMajorCourse() != 0) {
                searchResultTitleTv.setText(StringUtils.getString(R.string.major_course) + "(" + data.getTotalMajorCourse() + ")");
            } else {
                searchResultTitleTv.setText(StringUtils.getString(R.string.major_course));
            }
            if (ListUtil.isEmpty(data.getMajorCourseDBModelList())) {
                moreLayout.setVisibility(View.INVISIBLE);
                noResultTv.setVisibility(View.VISIBLE);
                mmMajorCourseAdapter.setNewData(new LinkedList<MultiItemEntity>());
            } else {
                if (data.getTotalMajorCourse() <= SearchResultItem.MAX_SAMPLE_NUM) {
                    moreLayout.setVisibility(View.INVISIBLE);
                } else {
                    moreLayout.setVisibility(View.VISIBLE);
                }
                noResultTv.setVisibility(View.INVISIBLE);
            }
            List<MultiItemEntity> destList = new LinkedList<>();
            destList.addAll(data.getMajorCourseDBModelList().subList(0, Math.min(data.getMajorCourseDBModelList().size(), SearchResultItem.MAX_SAMPLE_NUM)));
            mmMajorCourseAdapter.setNewData(destList);
        }
    }

    public class TraingClassViewHolder extends BaseViewHolder {
        @BindView(R.id.searchResultTitleTv)
        TextView searchResultTitleTv;
        @BindView(R.id.moreLayout)
        LinearLayout moreLayout;
        @BindView(R.id.recyclerView)
        RecyclerView recyclerView;
        @BindView(R.id.noResultTv)
        TextView noResultTv;
        MMTraingClassAdapter mmTraingClassAdapter;


        public TraingClassViewHolder(View view) {
            super(view);
            mmTraingClassAdapter = new MMTraingClassAdapter(mActivity);;
            ButterKnife.bind(this, view);
            recyclerView.setLayoutManager(new LinearLayoutManager(itemView.getContext()));
            mmTraingClassAdapter.bindToRecyclerView(recyclerView);
            mmTraingClassAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    switch (mmTraingClassAdapter.getItem(position).getProgressStatus()) {
                        case PublishUtil.PROGRESS_STATUS_SERVER_TRANSCODING:
                            ToastX.show(R.string.server_transcoding);
                            break;
                        default:
                            Router.start(mmTraingClassAdapter.getItem(position).getRouter());
                            break;
                    }
                }
            });
            addOnClickListener(R.id.moreLayout);
        }

        public void bindData(SearchResultItem data) {
            searchResultTitleTv.setText(StringUtils.getString(R.string.traing_class) + "(" + data.getTotalTraingClass() + ")");
            if (ListUtil.isEmpty(data.getTraingClassDBModelList())) {
                moreLayout.setVisibility(View.INVISIBLE);
                noResultTv.setVisibility(View.VISIBLE);
                mmTraingClassAdapter.setNewData(new LinkedList<TraingClassDBModel>());
            } else {
                if (data.getTotalTraingClass() <= SearchResultItem.MAX_SAMPLE_NUM) {
                    moreLayout.setVisibility(View.INVISIBLE);
                } else {
                    moreLayout.setVisibility(View.VISIBLE);
                }
                noResultTv.setVisibility(View.INVISIBLE);
            }
            mmTraingClassAdapter.setNewData(data.getTraingClassDBModelList().subList(0, Math.min(data.getTraingClassDBModelList().size(), SearchResultItem.MAX_SAMPLE_NUM)));
        }
    }

    public class TraingCardViewHolder extends BaseViewHolder {
        @BindView(R.id.searchResultTitleTv)
        TextView searchResultTitleTv;
        @BindView(R.id.moreLayout)
        LinearLayout moreLayout;
        @BindView(R.id.recyclerView)
        RecyclerView recyclerView;
        @BindView(R.id.noResultTv)
        TextView noResultTv;
        MMTraingCardAdapter mmTraingCardAdapter;

        public TraingCardViewHolder(View view) {
            super(view);
            mmTraingCardAdapter = new MMTraingCardAdapter(mActivity);
            ButterKnife.bind(this, view);
            recyclerView.setLayoutManager(new LinearLayoutManager(itemView.getContext()));
            mmTraingCardAdapter.bindToRecyclerView(recyclerView);
            mmTraingCardAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    switch (mmTraingCardAdapter.getItem(position).getProgressStatus()) {
                        case PublishUtil.PROGRESS_STATUS_SERVER_TRANSCODING:
                            ToastX.show(R.string.server_transcoding);
                            break;
                        default:
                            Router.start(mmTraingCardAdapter.getItem(position).getRouter());
                            break;
                    }
                }
            });
            recyclerView.setLayoutManager(new LinearLayoutManager(itemView.getContext()));
            addOnClickListener(R.id.moreLayout);
        }

        public void bindData(SearchResultItem data) {
            searchResultTitleTv.setText(StringUtils.getString(R.string.traing_card) + "(" + data.getTotalTraingCard() + ")");
            if (ListUtil.isEmpty(data.getTraingCardDBModelList())) {
                moreLayout.setVisibility(View.INVISIBLE);
                noResultTv.setVisibility(View.VISIBLE);
                mmTraingCardAdapter.setNewData(new LinkedList<TraingCardDBModel>());
            } else {
                if (data.getTotalTraingCard() <= SearchResultItem.MAX_SAMPLE_NUM) {
                    moreLayout.setVisibility(View.INVISIBLE);
                } else {
                    moreLayout.setVisibility(View.VISIBLE);
                }
                noResultTv.setVisibility(View.INVISIBLE);
            }
            mmTraingCardAdapter.setNewData(data.getTraingCardDBModelList().subList(0, Math.min(data.getTraingCardDBModelList().size(), SearchResultItem.MAX_SAMPLE_NUM)));
        }
    }

    public class PersonalTrainerViewHolder extends BaseViewHolder {
        @BindView(R.id.searchResultTitleTv)
        TextView searchResultTitleTv;
        @BindView(R.id.moreLayout)
        LinearLayout moreLayout;
        @BindView(R.id.recyclerView)
        RecyclerView recyclerView;
        @BindView(R.id.noResultTv)
        TextView noResultTv;

        public PersonalTrainerViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            recyclerView.setLayoutManager(new LinearLayoutManager(itemView.getContext()));
            searchResultTitleTv.setText(R.string.personal_trainer);
            addOnClickListener(R.id.moreLayout);
        }

        public void bindData(SearchResultItem data) {
            if (ListUtil.isEmpty(data.getLearnVideoModelList())) {
                moreLayout.setVisibility(View.INVISIBLE);
                noResultTv.setVisibility(View.VISIBLE);
            } else {
                moreLayout.setVisibility(View.VISIBLE);
                noResultTv.setVisibility(View.INVISIBLE);
            }
        }
    }

    public class UniversalProductViewHolder extends BaseViewHolder {
        @BindView(R.id.searchResultTitleTv)
        TextView searchResultTitleTv;
        @BindView(R.id.moreLayout)
        LinearLayout moreLayout;
        @BindView(R.id.recyclerView)
        RecyclerView recyclerView;
        @BindView(R.id.noResultTv)
        TextView noResultTv;

        public UniversalProductViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            recyclerView.setLayoutManager(new LinearLayoutManager(itemView.getContext()));
            searchResultTitleTv.setText(R.string.universal_product);
            addOnClickListener(R.id.moreLayout);
        }

        public void bindData(SearchResultItem data) {
            if (ListUtil.isEmpty(data.getLearnVideoModelList())) {
                moreLayout.setVisibility(View.INVISIBLE);
                noResultTv.setVisibility(View.VISIBLE);
            } else {
                moreLayout.setVisibility(View.VISIBLE);
                noResultTv.setVisibility(View.INVISIBLE);
            }
        }
    }

}
